package com.esp.app.brightness;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.esp.app.widget.R;

public class ViewBrightHandler {
	
	public static String TAG = ViewBrightHandler.class.getCanonicalName();
	
	public static void updateAppWidgets(final Context context,
			final AppWidgetManager appWidgetManager, final int[] appWidgetIds,
			final int resource) {

		Log.d(TAG, "ViewBrightHandler | updateAppWidgets :=>" + appWidgetIds.length + " widget ids");

		for (int i = 0; i < appWidgetIds.length; i++) {
			final int appWidgetId = appWidgetIds[i];
			updateAppWidget(context, appWidgetManager, appWidgetId, resource);
		}

	}
	
	public static void updateAppWidget(final Context context,
			final AppWidgetManager appWidgetManager, final int appWidgetId,
			final int resource) {

		Log.d(TAG, "ViewBrightHandler | updateAppWidget :=> " + appWidgetId);

		final RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		views.setImageViewResource(R.id.btn_brightness,resource);
		
		Intent intent = new Intent(context,BrightnessActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
        views.setOnClickPendingIntent(R.id.btn_brightness,pIntent);
		appWidgetManager.updateAppWidget(appWidgetId, views);

	}
}
